<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use App\Models\FsUser;
use Illuminate\Support\Facades\Auth;

class PhoneRecord extends Model
{
    protected $fillable = [
        'call_sid',
        'apps',
        'recording_url',
        'recording_time',
        'call_status',
        'recording_sid',
        'fs_user_id',
        'user_id',
        'task_id',
        'phone_setting_id',
        'created_at',
        'updated_at',
        'user_phone',
        'fs_user_phone'
    ];

    public $timestamps = false;

    protected $connection = 'mysql';

    public function user(){
        return $this->belongsTo('App\Models\User','user_id','id');
    }

    public function fsUser(){
        return $this->belongsTo('App\Models\FsUser','fs_user_id','id');
    }

    public function recordRemark(){
        return $this->hasMany('App\Models\RecordRemark','record_id','id');
    }

    public function search($name='',$username='',$true_name='',$start_recording_time='',$end_recording_time='',$created_at_time='',$updated_at_time='',$limit=10, $type='',$download=''){
        $builder = self::query();
        if($download){

        }else{
            //判断有无权限，返回所有或者当前
            if(!Auth::user()->hasPermissionTo('api.record.index.all')){
                $builder->where('user_id', Auth::user()->id);
            }
        }

        //电销人员账号（user表）
        if ($name){
            $like = '%'.$name.'%';
            $ids = User::where('name','like',$like)->pluck('id');
            $builder->whereIn('user_id',$ids);
        }
        //会员用户名 （fs_user）
        if ($username){
            $like = '%'.$username.'%';
            $ids = FsUser::where('username','like',$like)->pluck('id');
            $builder->whereIn('fs_user_id',$ids);
        }
        //会员真名
        if ($true_name){
            $like = '%'.$true_name.'%';
            $ids = FsUser::where('true_name','like',$like)->pluck('id');
            $builder->whereIn('fs_user_id',$ids);
        }
        //通话时长
        if($start_recording_time && $end_recording_time){
            $builder->where('recording_time', '>', $start_recording_time);
            $builder->where('recording_time', '<', $end_recording_time);
        }elseif ($start_recording_time){
            $builder->where('recording_time', '>', $start_recording_time);
        }elseif ($end_recording_time){
            $builder->where('recording_time', '<', $end_recording_time);
        }
        //通话时间范围
        if($created_at_time && $updated_at_time){
            $builder->where('created_at', '>', $created_at_time);
            $builder->where('updated_at', '<', $updated_at_time);
        }elseif ($created_at_time){
            $builder->where('created_at', '>', $created_at_time);
        }elseif ($updated_at_time){
            $builder->where('updated_at', '<', $updated_at_time);
        }
        $builder->orderByDesc('created_at');
        if($type == 1){
            return $builder;
        }
        return $builder->paginate($limit);
    }
}
